package com.right.health.app.dialog;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;

import com.magic.basic.utils.CollectionUtil;
import com.right.health.app.R;
import com.right.health.app.able.INameAble;
import com.right.health.app.widget.wheelView.adapter.ArrayWheelAdapter;
import com.right.health.app.widget.wheelView.view.WheelView;

import java.util.ArrayList;
import java.util.List;

/*
 * Created by hopeliao on 2019-10-13
 *
 */
public class BottomPickerDialog<T extends INameAble> extends  BaseBottomSheetDialog implements View.OnClickListener {

    private WheelView mPickerView;

    private OnChooseListener mChooseListener;

    private List<T> mDataSource = new ArrayList<>();

    public void setDataSource(List<T> source) {
        mDataSource.clear();
        if(!CollectionUtil.isEmpty(source)) {
            mDataSource.addAll(source);
        }
        mPickerView.setAdapter(new PickerAdapter(mDataSource));
        mPickerView.setCurrentItem(0);
    }

    public BottomPickerDialog(@NonNull Context context) {
        super(context);
    }

    public void setChooseListener(OnChooseListener listener) {
        this.mChooseListener = listener;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.dialog_picker;
    }

    @Override
    protected void findViews() {
        super.findViews();
        mPickerView = (WheelView) findViewById(R.id.picker_view);

        findViewById(R.id.view_placeholder).setOnClickListener(this);
        findViewById(R.id.tv_cancel).setOnClickListener(this);
        findViewById(R.id.tv_yes).setOnClickListener(this);

        mPickerView.setCyclic(false);
    }

    private class PickerAdapter extends ArrayWheelAdapter<T> {


        public PickerAdapter(List<T> items) {
            super(items);
        }

        @Override
        public Object getItem(int index) {
            if (index >= 0 && index < items.size()) {
                return items.get(index).getName();
            }
            return "";

        }
    }



    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.view_placeholder || v.getId() == R.id.tv_cancel) {
            dismiss();
        } else if(v.getId() == R.id.tv_yes) {
            if(mChooseListener != null) {
                mChooseListener.onChoose(mTag, mPickerView.getCurrentItem());
            }
        }
    }

    public static class BottomPickerInfo implements INameAble{
        public String name;

        public BottomPickerInfo(String name) {
            this.name = name;
        }

        @Override
        public String getName() {
            return name;
        }
    }

    public interface OnChooseListener {
        void onChoose(Object tag, int choosePosition);
    }
}
